package com.wyth.lottery.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wyth.lottery.R;
import com.wyth.lottery.base.BaseFragment;
import com.wyth.lottery.bean.hezhi.HezhiBeanNew;
import com.wyth.lottery.bean.twosamenumber.TwoSameNumbeTypeBean;
import com.wyth.lottery.bean.twosamenumber.TwoSameNumberBeanNew;
import com.wyth.lottery.utils.MathQT;
import com.wyth.lottery.utils.MyApplication;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/2 0002.
 */

public class TwoTongFragment extends BaseFragment {

    @BindView(R.id.rv_hezhi)
    RecyclerView rvHezhi;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.tv_notes_sum)
    TextView tvNotesSum;
    @BindView(R.id.tv_money_sum)
    TextView tvMoneySum;
    @BindView(R.id.tv_2notonghao_11)
    TextView tv2notonghao11;
    @BindView(R.id.tv_2notonghao_22)
    TextView tv2notonghao22;
    @BindView(R.id.tv_2notonghao_33)
    TextView tv2notonghao33;
    @BindView(R.id.tv_2notonghao_44)
    TextView tv2notonghao44;
    @BindView(R.id.tv_2notonghao_55)
    TextView tv2notonghao55;
    @BindView(R.id.tv_2notonghao_66)
    TextView tv2notonghao66;
    private int redcount = 0;
    private int sumHezhi;
    private int sumHezhiJine;
    private boolean tv11Enable = true;
    private boolean tv22Enable = true;
    private boolean tv33Enable = true;
    private boolean tv44Enable = true;
    private boolean tv55Enable = true;
    private boolean tv66Enable = true;
    private ArrayList<TwoSameNumberBeanNew> hezhi = new ArrayList<>();
    private ArrayList<TwoSameNumbeTypeBean> type = new ArrayList<>();
    private HezhiAdapter redAdapter;
    @Override
    protected View getSuccessView() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.fragment_qthree_2tonghao, null);
        return view;
    }

    @Override
    protected void initDate() {
        stateLayout.showSuccessView();
        initQThreeView();
    }

    private void initQThreeView() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);
        rvHezhi.setLayoutManager(staggeredGridLayoutManager);
        ArrayList<TwoSameNumberBeanNew> list;
        list = MathQT.twoTonghao();
        hezhi= list;
        redAdapter = new HezhiAdapter(list);
        rvHezhi.setAdapter(redAdapter);
    }

    @OnClick({R.id.tv_2notonghao_11, R.id.tv_2notonghao_22, R.id.tv_2notonghao_33, R.id.tv_2notonghao_44, R.id.tv_2notonghao_55, R.id.tv_2notonghao_66})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_2notonghao_11:
                if (tv11Enable){
                    tv2notonghao11.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao11.setTextColor(getResources().getColor(R.color.white));
                    tv11Enable = false;
                    initViewEnable(MathQT.twoTonghao11());
                }else {
                    tv2notonghao11.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao11.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv11Enable = true;
                    initViewNoEnable(MathQT.twoTonghao11());
                }
                break;
            case R.id.tv_2notonghao_22:
                if (tv22Enable){
                    tv2notonghao22.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao22.setTextColor(getResources().getColor(R.color.white));
                    tv22Enable = false;
                    initViewEnable(MathQT.twoTonghao22());
                }else {
                    tv2notonghao22.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao22.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv22Enable = true;
                    initViewNoEnable(MathQT.twoTonghao22());
                }
                break;
            case R.id.tv_2notonghao_33:
                if (tv33Enable){
                    tv2notonghao33.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao33.setTextColor(getResources().getColor(R.color.white));
                    tv33Enable = false;
                    initViewEnable(MathQT.twoTonghao33());
                }else {
                    tv2notonghao33.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao33.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv33Enable = true;
                    initViewNoEnable(MathQT.twoTonghao33());
                }
                break;
            case R.id.tv_2notonghao_44:
                if (tv44Enable){
                    tv2notonghao44.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao44.setTextColor(getResources().getColor(R.color.white));
                    tv44Enable = false;
                    initViewEnable(MathQT.twoTonghao44());
                }else {
                    tv2notonghao44.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao44.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv44Enable = true;
                    initViewNoEnable(MathQT.twoTonghao44());
                }
                break;
            case R.id.tv_2notonghao_55:
                if (tv55Enable){
                    tv2notonghao55.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao55.setTextColor(getResources().getColor(R.color.white));
                    tv55Enable = false;
                    initViewEnable(MathQT.twoTonghao55());
                }else {
                    tv2notonghao55.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao55.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv55Enable = true;
                    initViewNoEnable(MathQT.twoTonghao55());
                }
                break;
            case R.id.tv_2notonghao_66:
                if (tv66Enable){
                    tv2notonghao66.setBackgroundResource(R.drawable.text_red_10_background);
                    tv2notonghao66.setTextColor(getResources().getColor(R.color.white));
                    tv66Enable = false;
                    initViewEnable(MathQT.twoTonghao66());
                }else {
                    tv2notonghao66.setBackgroundResource(R.drawable.text_pink_10_background);
                    tv2notonghao66.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tv66Enable = true;
                    initViewNoEnable(MathQT.twoTonghao66());
                }
                break;
        }
    }
    private void initViewNoEnable(ArrayList<String> arrayList) {
        for (int i1 = 0; i1 < hezhi.size(); i1++) {

            TwoSameNumberBeanNew hezhiBeanNew = hezhi.get(i1);
            //hezhiBeanNew.selectEnable = true;
            for (int i = 0; i < arrayList.size(); i++) {

                String text = hezhiBeanNew.getTwoSameNumber();

                String s = arrayList.get(i);
                if (text.equals(s)) {
                    hezhiBeanNew.selectEnable = true;
                }
            }
        }
        redAdapter.setNewData(hezhi);
    }

    private void initViewEnable(ArrayList<String> arrayList) {
        for (int i1 = 0; i1 < hezhi.size(); i1++) {

            TwoSameNumberBeanNew hezhiBeanNew = hezhi.get(i1);
            //hezhiBeanNew.selectEnable = true;
            for (int i = 0; i < arrayList.size(); i++) {

                String text = hezhiBeanNew.getTwoSameNumber();

                String s = arrayList.get(i);
                if (text.equals(s)) {
                    hezhiBeanNew.selectEnable = false;
                }
            }
        }
        redAdapter.setNewData(hezhi);
    }
    class HezhiAdapter extends BaseQuickAdapter<TwoSameNumberBeanNew, BaseViewHolder> {

        public HezhiAdapter(List<TwoSameNumberBeanNew> data) {
            super(R.layout.layout_2tonghao, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final TwoSameNumberBeanNew item) {
            final LinearLayout linearLayout = helper.getView(R.id.ll_2tonghao);
            final TextView textView = helper.getView(R.id.tv_2tonghao);
            helper.setText(R.id.tv_2tonghao, item.getTwoSameNumber());
            if (item.selectEnable) {
                linearLayout.setBackgroundResource(R.drawable.text_pink_10_background);
                textView.setTextColor(getResources().getColor(R.color.colorTextSelect));
            } else {
                linearLayout.setBackgroundResource(R.drawable.text_red_10_background);
                textView.setTextColor(getResources().getColor(R.color.white));
            }
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.selectEnable) {
                        linearLayout.setBackgroundResource(R.drawable.text_red_10_background);
                        textView.setTextColor(getResources().getColor(R.color.white));
                        ++redcount;
                        item.selectEnable = false;
                        tvSum.setText(redcount + "");
                        tvNotesSum.setText(redcount + "");
                        tvMoneySum.setText(2 * redcount + "");
                    } else {
                        linearLayout.setBackgroundResource(R.drawable.text_pink_10_background);
                        textView.setTextColor(getResources().getColor(R.color.colorTextSelect));
                        --redcount;
                        item.selectEnable = true;
                        tvSum.setText(redcount + "");
                        tvNotesSum.setText(redcount + "");
                        tvMoneySum.setText(2 * redcount + "");
                    }
                    sumHezhi = MathQT.sumHezhi(redcount);
                    sumHezhiJine = MathQT.sumHezhiJine(redcount);
                }
            });
        }
    }
}
